// pages/staffregister/staffregister.js
import { getMerchantRoles, smsSend, merchantJoin } from '../../../utils/networkRequest.js'
import { errorMsg, getQueryString } from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    downtime: 60,//验证码倒计时 
    jobList: [{ name: "店长", id: 1 }, { name: "店员", id: 2 }],
    dataInfo: null,
    phoneNumber: "",
    roleIndex: "",
    code: "",
    mid: "",
    uid: "",
    t: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let mid, uid, t;
    if (options.scene) {
      let str = unescape(options.scene);
      mid = getQueryString('mid', str);
      uid = getQueryString('uid', str);
      t = getQueryString('t', str);
    } else {
      if (options) {
        mid = options.mid;
        uid = options.uid;
        t = options.t;
      }
    }
    this.setData({
      mid,
      uid,
      t
    })
    let self = this;
    if (mid && uid && t) {
      getMerchantRoles(mid, (r) => {
        if (r.code == 0) {
          self.setData({
            dataInfo: r.data
          })
        } else {
          errorMsg(r.message)
        }
      })
    }
  },

  onShow: function () {
    let { mid, uid, t } = this.data;
    let self = this;
    if (mid && uid && t) {
      getMerchantRoles(mid, (r) => {
        if (r.code == 0) {
          self.setData({
            dataInfo: r.data
          })
        } else {
          errorMsg(r.message)
        }
      })
    }
  },
  //获取人数
  bindRoleChange: function (e) {
    this.setData({
      roleIndex: e.detail.value
    })
  },

  //获取手机号
  phoneNumberChange: function (e) {
    this.setData({
      phoneNumber: e.detail.detail.value
    })
  },
  //获取验证码
  codeChange: function (e) {

    this.setData({
      code: e.detail.value
    })
  },
  //发送验证码
  onSendCode: function (e) {
    let self = this;
    let { phoneNumber } = this.data;
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phoneNumber)) {
      errorMsg('手机号不正确');
    } else {
      if (this.data.downtime == 60) {
        smsSend(phoneNumber, (r) => {

          if (r.code == 0) {
            this.setData({
              downtime: --self.data.downtime
            })
            let siv = setInterval(() => {
              if (self.data.downtime > 1) {
                self.setData({
                  downtime: --self.data.downtime
                })
              } else {
                clearInterval(siv);
                self.setData({
                  downtime: 60
                })
              }

            }, 1000)
          } else {
            errorMsg(r.message);
          }
        })
      }
    }
  },
  onSubmit: function () {
    let { phoneNumber, roleIndex, code, mid, uid, t, dataInfo } = this.data;
    if (mid && uid && t) {

      //dataInfo.roles[roleIndex].id  之前获取职位的代码
      merchantJoin(mid, uid, t, "", phoneNumber, code, (r) => {
        if (r.code == 0) {
          debugger;
          wx.showModal({
            title: '注册成功',
            showCancel: false,
            content: '注册成功，开始使用。',
            success: function (res) {
              if (res.confirm) {
                //跳转到首页 
                wx.reLaunch({
                  url: '../../index/index'
                })
              }
            }
          })
        } else {
          errorMsg(r.message)
        }
      })
    } else {
      errorMsg("未知来源")
    }
  }
})